--
-- call control lua script
--
aaa = argv[1];
bbb = argv[2];
dialA = "user/"..aaa
dialB = "user/"..bbb
legA = freeswitch.Session(dialA)
dispoA = "None"
while(legA:ready() and dispoA ~= "ANSWER") do
	dispoA = legA:getVariable("endpoint_disposition")
	freeswitch.consoleLog("NOTICE", "Leg A disposition is '" .. dispoA .. "'\n")
	os.execute("sleep 1")
end -- legA while
if(not legA:ready()) then
	-- oops, lost leg A handle this case
	freeswitch.consoleLog("NOTICE", "\n----------------It appears that " .. dialA .. " disconnected...\n")
else
	legB = freeswitch.Session(dialB)
	dispoB = "None"
	while(legA:ready() and legB:ready() and dispoB ~= "ANSWER") do
		if(not legA:ready()) then
			-- oops, leg A hung up or got disconnected, handle this case
			freeswitch.consoleLog("NOTICE", "\n----------------It appears that " .. dialA .. " disconnected...\n")
		else
			os.execute("sleep 1")
			dispoB = legB:getVariable("endpoint_disposition")
			freeswitch.consoleLog("\n----------------NOTICE", "Leg B disposition is '" .. dispoB .. "'\n")
		end -- inner if legA ready
	end -- legB while
	if(legA:ready() and legB:ready()) then
		
		legA:execute("record_session", "/tmp/record/" .. legA:get_uuid() .. ".wav")
		freeswitch.bridge(legA, legB)
		freeswitch.consoleLog("NOTICE", "\n--------------bridge--\n")
		
	else
		-- oops, one of the above legs hung up, handle this case
		freeswitch.consoleLog("\n----------------NOTICE", "It appears that " .. dialA .. " or " .. dialB .. " disconnected...\n")
	end
end -- outter if legA ready
